<?php
/**
 * User: Allen
 * Date: 15-11-25
 * 佣金日志控制器
 */
class System_money_rizhi extends PC_Controller {
	protected $go_url = '/admin/system_money_rizhi/index';
	public function __construct() {
		parent::__construct ();
		$this->load->model ( 'system_money_rizhi_model' );
	}
	
	/**
	 * @brief 佣金日志信息页面
	 * 
	 * @param
	 *        	Null
	 *        	@par 2015/11/25 Ver 1.00 Created by Allen
	 */
	public function index() {
		$message = 1;
		if ($this->uri->segment ( 4 )) {
			$message = $this->uri->segment ( 4 );
		}
		$this->data ['username'] = isset ( $_POST ['username'] ) ? $_POST ['username'] : '';
		$this->data ['select_type'] = isset ( $_POST ['select_type'] ) ? $_POST ['select_type'] : 'or';
		$this->data ['start_time'] = isset ( $_POST ['start_time'] ) ? $_POST ['start_time'] : '';
		$this->data ['end_time'] = isset ( $_POST ['end_time'] ) ? $_POST ['end_time'] : '';
		$this->data ['message'] = $message;
		// 判断权限
		$this->data ['authority'] = $this->authority;
		$this->load->view ( '/admin/system_money_rizhi_list.php', $this->data );
	}
	
	/**
	 * @brief ajxa获取佣金日志信息
	 * 
	 * @param
	 *        	Null
	 *        	@par 2015/11/25 Ver 1.00 Created by Allen
	 */
	public function ajax_money_log_list() {
		$data = $_GET;
		$start = $data ['iDisplayStart']; // 显示的起始索引
		$length = $data ['iDisplayLength']; // 显示的行数
		$sort_th = $data ['mDataProp_' . $data ['iSortCol_0'] . '']; // 被排序的列
		$sort_type = $data ['sSortDir_0']; // 排序的方向 "desc" 或者 "asc".
		$search = $data ['sSearch']; // 全局搜索字段
		$where = '1=1 ';
		$userid_where='';
		if ($this->admin ['admin_type'] == 'user') {
			$where.=' and user_id='.$this->admin['id'];
		}
		if ($this->admin ['admin_type'] == 'admin') {
			$userid_where = '';
			if (isset ( $data ['username'] ) && $data ['username']) {
				$sql = "SELECT id FROM user WHERE locate ('" . $data ['username'] . "' , user.username) > 0";
				$query = $this->db->query ( $sql );
				$user_ids = array ();
				foreach ( $query->result () as $row ) {
					$user_ids [] = $row->id;
				}
				$userid_where = " user_id in(" . implode ( ',', $user_ids ) . ")";
				unset ( $data ['username'] );
			}
		}
		$data_where = '';
		if (isset ( $data ['start_time'] ) && $data ['start_time']) {
			$data_where = ' create_time >=' . strtotime ( $data ['start_time'] );
			unset ( $data ['start_time'] );
		}
		if (isset ( $data ['end_time'] ) && $data ['end_time']) {
			$data_where .= ' and create_time <=' . strtotime ( $data ['end_time'] );
			unset ( $data ['end_time'] );
		}
		if ($data_where && $userid_where) {
			$where .= 'and (' . $userid_where . ' ' . $data ['select_type'] . ' ' . $data_where . ')';
		} else if ($data_where) {
			$where .= ' and ' . $data_where;
		} else if ($userid_where) {
			$where .= ' and ' . $userid_where;
		}
		
		$this->data ['count'] = $this->system_money_rizhi_model->getCount ( $where );
		$aaData = array ();
		if ($this->data ['count']) {
			$order_by = $sort_th . " " . $sort_type;
			$money_list = $this->system_money_rizhi_model->getSystemMoneyRizhiList ( $where, $length, $start, $order_by );
			foreach ( $money_list as $key => $value ) {
				$type_name = '';
				switch ($value ['type']) {
					case 'direct' :
						$type_name = '直推';
						break;
					case 'concur' :
						$type_name = '互助';
						break;
					case 'range' :
						$type_name = '极差';
						break;
					case 'welfare' :
						$type_name = '福利';
						break;
					case 'tixian' :
						$type_name = '提现';
						break;
					case 'tax' :
						$type_name = '税';
						break;
					case 'feiyong' :
						$type_name = '维护费';
						break;
					case 'zhuan' :
						$type_name = '转账';
						break;
					case 'adduser' :
						$type_name = '报单';
						break;
					case 'bonus' :
					    $type_name = '分红';
					    break;
				    case 'guquan' :
				        $type_name = '股权';
				        break;
					default :
					    $type_name=$value['type'];
						break;
				}
				if(!$value['user_level_name']){
					$value['user_level_name']='无';
				}
				if(!$value['from_user_name']){
					$value['from_user_name']='无';
				}
				$money_list [$key] ['type'] = $type_name;
				$money_list [$key] ['create_time'] = date ( "Y-m-d H:i:s", $value ['create_time'] );
			}
			$aaData = $money_list;
		}
		$output ['aaData'] = $aaData;
		$output ['sEcho'] = $_GET ['sEcho'];
		$output ['iTotalDisplayRecords'] = $this->data ['count']; // 总共有几条数据
		$output ['iTotalRecords'] = $this->data ['count']; // 总共有几条数据
		echo json_encode ( $output ); // 最后把数据以json格式返回
	}
}
?>